<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="报到位置" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['locatin', validatorRules.locatin]" placeholder="请输入报到位置"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="司机姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['driverName', validatorRules.driverName]" placeholder="请输入司机姓名"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['idCard', validatorRules.idCard]" placeholder="请输入身份证号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['phoneNum', validatorRules.phoneNum]" placeholder="请输入手机号码"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车牌颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['plateColor', validatorRules.plateColor]" :trigger-change="true" dictCode="cpys" placeholder="请选择车牌颜色" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车牌号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['plateNo', validatorRules.plateNo]" placeholder="请输入车牌号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车辆类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['carType', validatorRules.carType]" :trigger-change="true" dictCode="cllx" placeholder="请选择车辆类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="排放类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['evnType', validatorRules.evnType]" :trigger-change="true" dictCode="pflx" placeholder="请选择排放类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="VIN识别码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['vin', validatorRules.vin]" placeholder="请输入VIN识别码"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发动机号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['engineNum', validatorRules.engineNum]" placeholder="请输入发动机号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="注册日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择注册日期" v-decorator="['registerDate', validatorRules.registerDate]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="所属车队" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fleet', validatorRules.fleet]" placeholder="请输入所属车队"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="运输内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['goods', validatorRules.goods]" placeholder="请输入运输内容"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="行驶证" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple  v-decorator="['lisense', validatorRules.lisense]" ></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="环保清单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple  v-decorator="['inventory', validatorRules.inventory]" ></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="登记状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['statusLedger']" :trigger-change="true" dictCode="tzzt" placeholder="请选择登记状态" />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CarRegisterForm',
    components: {
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          locatin: {
            rules: [
              { required: true, message: '请输入报到位置!'},
            ]
          },
          driverName: {
            rules: [
              { required: true, message: '请输入司机姓名!'},
            ]
          },
          idCard: {
            rules: [
              { required: true, message: '请输入身份证号!'},
            ]
          },
          phoneNum: {
            rules: [
              { required: true, message: '请输入手机号码!'},
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
            ]
          },
          plateColor: {
            rules: [
              { required: true, message: '请输入车牌颜色!'},
            ]
          },
          plateNo: {
            rules: [
              { required: true, message: '请输入车牌号!'},
            ]
          },
          carType: {
            rules: [
              { required: true, message: '请输入车辆类型!'},
            ]
          },
          evnType: {
            rules: [
              { required: true, message: '请输入排放类型!'},
            ]
          },
          vin: {
            rules: [
              { required: true, message: '请输入VIN识别码!'},
            ]
          },
          engineNum: {
            rules: [
              { required: true, message: '请输入发动机号!'},
            ]
          },
          registerDate: {
            rules: [
              { required: true, message: '请输入注册日期!'},
            ]
          },
          fleet: {
            rules: [
              { required: true, message: '请输入所属车队!'},
            ]
          },
          goods: {
            rules: [
              { required: true, message: '请输入运输内容!'},
            ]
          },
          lisense: {
            rules: [
              { required: true, message: '请输入行驶证!'},
            ]
          },
          inventory: {
            rules: [
              { required: true, message: '请输入环保清单!'},
            ]
          },
        },
        url: {
          add: "/carinfo/carRegister/add",
          edit: "/carinfo/carRegister/edit",
          queryById: "/carinfo/carRegister/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'locatin','driverName','idCard','phoneNum','plateColor','plateNo','carType','evnType','vin','engineNum','registerDate','fleet','goods','lisense','inventory','status','statusLedger'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'locatin','driverName','idCard','phoneNum','plateColor','plateNo','carType','evnType','vin','engineNum','registerDate','fleet','goods','lisense','inventory','status','statusLedger'))
      },
    }
  }
</script>